﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;

namespace WoChu.RfClient.Duty
{
    public partial class FrmGoodsInfo : Form
    {
        public FrmGoodsInfo()
        {
            InitializeComponent();
            initDataGridSyInfo();
        }

        #region Form事件
        /// <summary>
        /// 扫描枪扫描事件
        /// </summary>
        /// <param name="sender"></p
        private void txtShopNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            initDataGridSyInfo();
            string goodsCode = this.txtShopNo.Text.ToString().Trim();
            List<LocationGoodsInventoryInfo> list = null;
            if (goodsCode != "")
            {
                try
                {
                    if (goodsCode.Length > 8)//非货位号
                    {
                        if (goodsCode.Substring(0,1)=="0" && goodsCode.Length == 18)
                        {
                            goodsCode = goodsCode.Substring(0, 10);
                        }
                        else if(goodsCode.Substring(0,3)=="WOC" && (goodsCode.Length==17 || goodsCode.Length==20))
                        {
                            goodsCode = goodsCode.Substring(0, goodsCode.Length - 8);
                        }

                        list = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByCode(goodsCode);
                    }
                    else
                    {
                        list = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoods(goodsCode);
                    }
                    if (list != null && list.Count > 0)
                    {
                        DataSet dsDG = new DataSet();
                        DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (i > 0)
                            {
                                int numner = 0;
                                for (int j = 0; j < dt.Rows.Count; j++)
                                {
                                    if (list[i].INVGOODSCODE.ToString() == dt.Rows[j]["商品编号"].ToString())
                                    {
                                        numner = 1;
                                        dt.Rows[j]["库存数量"] = Convert.ToDouble(dt.Rows[j]["库存数量"].ToString()) + Convert.ToDouble(list[i].QTY.ToString());
                                    }
                                }
                                if (numner != 1)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr = dt.NewRow();
                                    List<LocationGoodsInventoryDetail> listCreateDate = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByGoodsId(list[i].GOODSID.ToString());
                                    if (listCreateDate != null && listCreateDate.Count > 0)
                                    {
                                        dr["首次上架"] = listCreateDate[0].CREATETIME.ToString();
                                        dr["最近上架"] = listCreateDate[listCreateDate.Count - 1].CREATETIME.ToString();
                                    }
                                    else
                                    {
                                        dr["首次上架"] = "";
                                        dr["最近上架"] = "";
                                    }
                                    dr["货位"] = list[i].LOCATIONCODE.ToString();
                                    dr["商品编号"] = list[i].INVGOODSCODE.ToString();
                                    dr["商品名称"] = list[i].INVGOODSNAME.ToString(); 
                                    dr["单位"] = list[i].INVUNITNAME.ToString();
                                    dr["库存数量"] = list[i].QTY.ToString();
                                    dt.Rows.Add(dr);
                                }

                            }
                            else
                            {
                                DataRow dr = dt.NewRow();
                                dr = dt.NewRow();
                                List<LocationGoodsInventoryDetail> listCreateDate = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByGoodsId(list[i].GOODSID.ToString());
                                if (listCreateDate != null && listCreateDate.Count > 0)
                                {
                                    dr["首次上架"] = listCreateDate[0].CREATETIME.ToString();
                                    dr["最近上架"] = listCreateDate[listCreateDate.Count - 1].CREATETIME.ToString();
                                }
                                else
                                {
                                    dr["首次上架"] = "";
                                    dr["最近上架"] = "";
                                }
                                dr["货位"] = list[i].LOCATIONCODE.ToString();
                                dr["商品编号"] = list[i].INVGOODSCODE.ToString();
                                dr["商品名称"] = list[i].INVGOODSNAME.ToString();

                                dr["单位"] = list[i].INVUNITNAME.ToString();
                                dr["库存数量"] = list[i].QTY.ToString();
                                dt.Rows.Add(dr);
                            }

                        }
                        dsDG.Tables.Add(dt);
                        this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                    }
                    else
                    {
                        if (goodsCode.Length > 6)
                        {
                            MessageBox.Show("此商品编号暂无数据!");
                        }
                        else
                        {
                            MessageBox.Show("此货位号暂无数据!");
                        } 
                    }
                }
                catch (Exception)
                {

                    MessageBox.Show("此货位号暂无数据!");
                }


            }
            else
            {
                MessageBox.Show("请先输入产品编号或者货位号!");
            }
            this.txtShopNo.Text = "";
        }


        
        #endregion


        /// <summary>
        /// 初始化数据源表头
        /// </summary>
        private void initDataGridSyInfo()
        {
            DataSet ds = new DataSet();//创建DataSet  

            DataTable dt = new DataTable("ProfitAndLossInfo");//创建一个名为ProfitAndLossInfo的DataTalbe  

            dt.Columns.Add(new DataColumn("货位", typeof(string)));
            dt.Columns.Add(new DataColumn("商品编号", typeof(string)));//为dt_dry表内建立Column  
            dt.Columns.Add(new DataColumn("商品名称", typeof(string)));  
            dt.Columns.Add(new DataColumn("库存数量", typeof(string)));
            dt.Columns.Add(new DataColumn("单位", typeof(string)));
            dt.Columns.Add(new DataColumn("首次上架", typeof(string)));
            dt.Columns.Add(new DataColumn("最近上架", typeof(string)));
            
            ds.Tables.Add(dt);
            this.dataGridSyInfo.DataSource = ds.Tables[0];
        }

    }
}